package com.zl.aop.spring;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.ThrowsAdvice;

import java.lang.reflect.Method;

/**
 * @author ZLL
 * @date create in 2018/05/09
 */
public class AopAspect implements MethodBeforeAdvice, MethodInterceptor,
        AfterReturningAdvice, ThrowsAdvice{

    public void afterThrowing(Method m, Object[] args, Object target, RuntimeException ex) {
        System.out.println("AfterThrowsAdvice[" + ex.getMessage() + "]");
    }

    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("AroundAdvice[before]");
        Object ret=methodInvocation.proceed();
        System.out.println("AroundAdvice[after|"+ret+"]");
        return ret;
    }

    @Override
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("AfterReturnAdvice[" + o + "]");
    }

    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("BeforeAdvice");
    }
}
